/*
* Copyright (c) 2008 Matt Giger http://earthbrowser.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
* ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

/**
* ZoomCtl
*
* Control the camera zoom with a graphical slider with buttons
* 
*/
function ZoomCtl(plugin, properties)
{
	// Get into the plugin namespace
	with(plugin)
	{
		// A button skin defines the contents of the skin image for use in creating the control
		addSkin(new Skin("zoom", "zoom_skin.png", {
			thumb:		{ x:80, y:0, width:20, height:13 },
			zin:		{ x:40, y:0, width:38, height:30 },
			zout:		{ x:40, y:150, width:38, height:30 },
			zin_off:	{ x:0, y:0, width:38, height:30 },
			zout_off:	{ x:0, y:150, width:38, height:30 },
			back:{
				x:40, y:40,
				elements:[
					{ x:0, y:0, width:38, height:1 },
					{ x:0, y:1, width:38, height:16 },
					{ x:0, y:17, width:38, height:1 }
				]
			}
		}));
		
		// Get a reference to the camera for later
		var camera = getCamera();
		
		// Containing Pane
		var base = new Pane({width:38, height:180});
		
		// Add user specialized properties
		if(properties != undefined)
			base.setProperties(properties);
		
		// Add a Slider control
		var slider = base.addPane(new Slider({ invert:false, vertical:true }));
		
		// Set the Slider thumb
		var thumb = slider.setThumb(new Icon({skin:"zoom:thumb", x:10, binding:BIND_LEFT}));
		
		// Set the slider back
		var back = slider.setBack(new VBar({ skin:"zoom:back", width:38, height:120, binding:BIND_LEFT}));
		slider.moveTo(0, 30);
		slider.resizeTo(38, 120);
		
		// Put inactive button caps on the end of the zoom slider
		var zin_off = base.addPane(new Icon({skin:"zoom:zin_off", x:0, y:0, binding:BIND_LEFT}));
		var zout_off = base.addPane(new Icon({skin:"zoom:zout_off", x:0, y:150, binding:BIND_BOTTOM|BIND_LEFT}));
		
		// Zoom in button cap
		var zin = base.addPane(new Icon({skin:"zoom:zin", x:0, y:0, visible:false, binding:BIND_LEFT}));
		zin.addListener("mousedown", function(e) {
			camera.setZoomLevel(camera.getZoomLevel() - 0.1);
		});
		
		// Zoom out button cap
		var zout = base.addPane(new Icon({skin:"zoom:zout", x:0, y:150, visible:false, binding:BIND_BOTTOM|BIND_LEFT}));
		zout.addListener("mousedown", function(e) {
			camera.setZoomLevel(camera.getZoomLevel() + 0.1); 
		});

		// Hilight/Unhilight the active button caps on mouseover/mouseout
		base.addListener("mouseover", function(e) {
			zin.setVisible(true);
			zout.setVisible(true);
			zin_off.setVisible(false);
			zout_off.setVisible(false);
		});
		base.addListener("mouseout", function(e) {
			zin.setVisible(false);
			zout.setVisible(false);
			zin_off.setVisible(true);
			zout_off.setVisible(true);
		});
		
		// Listen for the camera zoom to change the slider value
		camera.addListener("onzoom", function(e) {
			slider.setValue(camera.getZoomLevel());
		});
		
		// listen for the slider value to change the zoom level
		slider.addListener("onslider", function(e) {
			camera.setZoomLevel(slider.getValue());
		});
	}
	
	return base;
}
